package cn.itcast.bos.service.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.CourierRepository;
import cn.itcast.bos.dao.FixedAreaRepository;
import cn.itcast.bos.dao.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.CourierService;
import cn.itcast.bos.service.FixedAreaService;
import cn.itcast.bos.service.TakeTimeService;
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {
	/*
	 * 添加操作
	 * (non-Javadoc)
	 * @see cn.itcast.bos.service.FixedAreaService#save(cn.itcast.bos.domain.base.FixedArea)
	 */
	//注入Dao
	@Autowired
	private FixedAreaRepository fixedAreaRepository; 
	@Override
	public void save(FixedArea model) {
		fixedAreaRepository.save(model);
	}
	@Override
	public Page<FixedArea> findPageData(Specification<FixedArea> specification,
			Pageable pageable) {
		return fixedAreaRepository.findAll(specification, pageable);
	}
	//定区关联快递员，关联时间
	@Autowired
	private CourierRepository courierRepository;
	@Autowired
	private TakeTimeRepository takeTimeRepository;
	@Override
	public void fixedArea_associationCourierToFixedArea(FixedArea model, Integer courierId, Integer takeTimeId) {
		//业务的主要思想，定区关联快递员，快递员关联时间
		//查询我们定区通过id
		String fixedId = model.getId();
		FixedArea fixedArea = fixedAreaRepository.findOne(fixedId);
		//查询我们的快递员
		Courier courier = courierRepository.findOne(courierId);
		//将快递员关联定区
		fixedArea.getCouriers().add(courier);
		//查询时间
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		//将时间set到我们的快递员中
		courier.setTakeTime(takeTime);
		System.out.println(fixedArea+"          "+courier+"    "+takeTime);
	}
	//查询所有的
	@Override
	public List<FixedArea> findAllFixedArea() {
		return fixedAreaRepository.findAll();
	}
}
